/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header$
 * $Id$
 * $Author$
 * $Date$
 * $Revision$
 */
package com.elancom.framework.test;

import java.io.IOException;
import java.net.URL;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;

/**
 * @author elancom
 * @date 2012-7-18
 */
public class AutoLoadMappingLocalSessionFactoryBean extends LocalSessionFactoryBean {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.orm.hibernate4.LocalSessionFactoryBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws IOException {
		String jarPath = null;
		ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
		// test
		ClassPathResource classPathResource = new ClassPathResource("com/elancom/platform/domain", resourcePatternResolver.getClassLoader());
		
		try {
			URL url = classPathResource.getURL();
			jarPath = url.getPath();
			int cidx = -1;
			if (jarPath != null && (cidx = jarPath.indexOf("!")) != -1) {
				int fidx = jarPath.indexOf("file:/");
				if (fidx != -1) {
					jarPath = jarPath.substring(fidx + 6);
				}
				jarPath = jarPath.substring(0, cidx - 6);
			}
		} catch (Exception e) {
			;// e.printStackTrace();
		}

		if (jarPath != null) {
			setMappingJarLocations(new Resource[] { new FileSystemResource(jarPath) });
		}

		super.afterPropertiesSet();
	}

}
